package com.zhang.service.impl;

import com.zhang.mapper.EmployeeMapper;
import com.zhang.pojo.Employee;
import com.zhang.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author zhangshihao
 * @create 2023-03-03 11:14
 */
@Service
public class EmployeeImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void save(Employee employee) {
        Collection<Employee> employees = employeeMapper.getAll();
        List<Integer> idList = new ArrayList<>();
        for (Employee emp : employees) {
            idList.add(emp.getId());
        }
        if (employee.getId() == null) {
            Integer initId = Collections.max(idList);
            employee.setId(++initId);
        }
        employeeMapper.save(employee);

    }

    @Override
    public Collection<Employee> getAll() {
        // 查询全部员工信息
        return employeeMapper.getAll();
    }

    @Override
    public Employee getEmployeeById(Integer id) {
        return employeeMapper.getEmployeeById(id);
    }

    @Override
    public void delete(Integer id) {
        employeeMapper.delete(id);
    }

    @Override
    public void update(Employee employee) {
        employeeMapper.update(employee);
    }
}
